package com.cn.onetrip.adapter;

import java.util.ArrayList;
import java.util.Vector;

import com.cn.onetrip.objects.VideosMediaObj;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;
import com.cn.onetrip.activity.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class VideosGridAdapter extends BaseAdapter {

	public Vector<VideosMediaObj> mModels = new Vector<VideosMediaObj>();
	private LayoutInflater mInflater;
	private Context mContext;
	private SyncImageLoader syncImageLoader;

	public VideosGridAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		syncImageLoader = new SyncImageLoader(context);
	}

	public void addVideos(String imageUrl, String cover, Integer duration,
			String name) {
		VideosMediaObj model = new VideosMediaObj();
		model.mediaUrl = imageUrl;
		model.cover = cover;
		model.duration = duration;
		model.name = name;
		mModels.add(model);
	}

	public void clean() {
		mModels.clear();
	}

	public void threadQuit() {
		syncImageLoader.threadQuit();
	}

	public int getCount() {
		return mModels.size();
	}

	public Object getItem(int position) {
		return mModels.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final ItemViewTag viewTag;
		VideosMediaObj model = mModels.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_item_video, null);

			// construct an item tag
			viewTag = new ItemViewTag(
					(RelativeLayout) convertView.findViewById(R.id.grid_id),
					(ImageView) convertView.findViewById(R.id.grid_video_play),
					(TextView) convertView.findViewById(R.id.grid_video_time),
					(TextView) convertView.findViewById(R.id.grid_video_title));
			convertView.setTag(viewTag);
		} else {
			viewTag = (ItemViewTag) convertView.getTag();
		}
		//
		// syncImageLoader.loadImage(model.cover, new OnImageLoadListener() {
		//
		// @Override
		// public void onImageLoad(Drawable drawable) {
		// if (drawable != null) {
		// viewTag.mIcon.setBackgroundDrawable(drawable);
		// // BitmapDrawable bitmapDrawable = (BitmapDrawable)
		// // drawable;
		// // Bitmap bitmap = bitmapDrawable.getBitmap();
		// // viewTag.mIcon.setImageBitmap(ActivityTag.toRoundCorner(
		// // bitmap, 50));
		// }
		// }
		//
		// @Override
		// public void onImageLoad(Integer t, Drawable drawable) {
		//
		// }
		//
		// @Override
		// public void onError() {
		//
		// }
		//
		// @Override
		// public void onError(Integer t) {
		//
		// }
		// });
		viewTag.mTitle.setText(model.name);
		viewTag.mName.setText(formatTime(model.duration));
		return convertView;
	}

	class ItemViewTag {
		protected RelativeLayout gridLayout;
		protected ImageView mIcon;
		protected TextView mName;
		protected TextView mTitle;

		/**
		 * The constructor to construct a navigation view tag
		 * 
		 * @param name
		 *            the name view of the item
		 * @param size
		 *            the size view of the item
		 * @param icon
		 *            the icon view of the item
		 */
		public ItemViewTag(RelativeLayout gridLayout, ImageView icon,
				TextView name, TextView title) {
			this.gridLayout = gridLayout;
			this.mName = name;
			this.mIcon = icon;
			this.mTitle = title;
		}
	}

	private String formatTime(int duration) {
		String timeFormat = String.format("%02d'%02d\"", duration / 60,
				duration % 60);
		return timeFormat;
	}

}